<?php
namespace app\admin\controller\auth;

use app\BaseController;
use app\common\business\auth\CommonAdminAuth;
use app\common\business\auth\CommonAdminMenu as serviceModel;
use app\common\validate\auth\CommonAdminMenu as validateModel;
use think\exception\ValidateException;

class CommonAdminMenu extends BaseController
{

    /*
     * 【方法注释】获取列表数组
     * 【参数定义】page：页码
     * 【参数定义】word：关键词，菜单名称或路径
     * 【方法返回】数据列表和总数据条数
     */
    public function list(){
        $fildes = 'page,word';
        $receiveData = getData($this->request->post(),$fildes);
        if(empty($receiveData['page'])){
            $receiveData['page'] = 1;
        }
        $where = [];
        $where[] = ['status','in',[0,1]];
        if(!empty($receiveData['word'])){
            $where[] = ['title|path','like','%'.$receiveData['word'].'%'];
        }
        $limit_start = ($receiveData['page']-1) * env('page_size');
        $data = [
            'lists' => serviceModel::getList($where,'*',$limit_start,env('page_size')),
            'allpage' => serviceModel::getCount($where)
        ];
        jsonSuccess($data);
    }

    /*
    * 【方法注释】加载菜单，根据不同角色加载对应的菜单
    * 【参数定义】token：用户唯一标识
    * 【方法返回】对应角色所有菜单栏目
    */
    public function listAll(){
        $admin = session('admin_user');
        //查询可访问的菜单
        $menu = CommonAdminAuth::getOne(['role_id'=>$admin['role_id']]);
        if ($admin['role_id'] == 1 && empty($menu['menu_select_ids'])) {
            $list = serviceModel::vipMenu();//超级管理员
        } else {
            if(empty($menu)){
                $list = [];
            }else{
                $list = serviceModel::selectMenu($admin['role_id']);
            }
        }
        jsonSuccess($list);
    }

    /*
     * 【方法注释】修改和编辑
     * 【参数定义】id：修改时数据ID
     * 【参数定义】title：菜单名称
     * 【参数定义】path：菜单路径
     * 【参数定义】parent_id：父级菜单ID
     * 【参数定义】order_num：排序值
     * 【参数定义】status：状态
     * 【参数定义】token：用户唯一标识
     * 【方法返回】返回是否成功即可
     */
    public function edit(){
        $fildes = "id,title,path,status,parent_id,order_num";
        $receiveData = getData($this->request->post(),$fildes);
        $admin = session('admin_user');
        try {
            if(!empty($receiveData['id'])){
                validate(validateModel::class)->scene('edit')->check($receiveData);
            }else{
                validate(validateModel::class)->scene('add')->check($receiveData);
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            jsonFail($e->getError());
        }
        $parent_id = empty($receiveData['parent_id']) ? 0 : $receiveData['parent_id'];
        $load = serviceModel::getOne(['title'=>$receiveData['title'],'parent_id'=>$parent_id]);
        if(!empty($receiveData['id'])){
            if(!empty($load) && $load['id'] != $receiveData['id']){
                jsonFail('已存在相同菜单名称');
            }
            serviceModel::edit($receiveData);
        }else{
            if(!empty($load)){
                jsonFail('已存在相同菜单名称');
            }
            serviceModel::insert($receiveData,$admin['username'].'/'.$admin['mobile']);
        }
        jsonSuccess();
    }

    /*
     * 【方法注释】删除数据
     * 【参数定义】id：删除时数据ID
     * 【参数定义】token：用户唯一标识
     * 【方法返回】返回是否成功即可
     */
    public function delete(){
        $fildes = "id";
        $receiveData = getData($this->request->post(),$fildes);
        $load = serviceModel::getOne(['id'=>$receiveData['id']]);
        if(empty($load)){
            jsonFail('删除的菜单不存在');
        }
        if($load['is_load'] == 0){
            jsonFail('该菜单不可删除');
        }
        serviceModel::updateStatus(-1,$load);
        jsonSuccess();
    }

    /*
     * 【方法注释】父级菜单所有数据
     * 【方法返回】返回父级菜单列表
     */
    public function parentList(){
        $data = [
            'lists' => serviceModel::getList(['parent_id'=>0],'id,title',0,100),
        ];
        jsonSuccess($data);
    }

}
